package com.hey.baseapplication.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.PersistableBundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.hey.baseapplication.R;
import com.hey.baseapplication.bean.User;
import com.hey.baseapplication.presenter.LoginPresenter;
import com.hey.baseapplication.utils.SPUtil;
import com.hey.baseapplication.view.ILoginView;
import com.hey.baseapplication.view.widget.ClearEditText;

import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class LoginActivity extends BaseActivity<LoginPresenter> implements ILoginView  {
    private ClearEditText username,password;
    private ImageView bg;
    private Button login;
    private TextView forgetPassword;
    private static  final String TAG="LoginActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setIsActionBar(false);
        setContentView(R.layout.activity_login);
        initView();
        Log.e(TAG,"onCreate");
    }

    @Override
    protected LoginPresenter initPresenter() {
        Log.e(TAG,"initPresenter");
      return new LoginPresenter(this);
    }

    @Override
    protected void findView() {
        setTitle("Login");
        username=f(R.id.username);
        password=f(R.id.password);
        login=f(R.id.login);
        bg=f(R.id.login_bg);
        forgetPassword=f(R.id.forget_password);
        Log.e(TAG,"findView");
    }

    @Override
    protected void initData() {
//        Observable.timer(200, TimeUnit.MILLISECONDS).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Long>() {
//            @Override
//            public void call(Long aLong) {
//
//            }
//        });

        Glide.with(LoginActivity.this).load(R.drawable.de_bg).into(new SimpleTarget<GlideDrawable>() {
            @Override
            public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                bg.setImageDrawable(resource);
                Animation animation = AnimationUtils.loadAnimation(LoginActivity.this, R.anim.translate_anim);
                bg.startAnimation(animation);
            }
        });
    }

    @Override
    protected void setListener() {
        Log.e(TAG,"setListener");
        login.setOnClickListener(this);
        forgetPassword.setOnClickListener(this);
    }

    @Override
    public String getUserName() {
        return username.getText().toString();
    }

    @Override
    public String getPassword() {
        return password.getText().toString();
    }

    @Override
    public void loginSuccess(User user) {
        showToast("Login Success");
        SPUtil.instance.loginSuccess(this);
        next();
    }

    @Override
    public void loginFailed(String err) {
        showToast("Login Failed");
    }

    @Override
    public void showLoading() {

    }

    @Override


    public void hideLoading() {

    }

    @Override
    public void next() {
        Intent intent=new Intent(this,MainActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.login:
                if (TextUtils.isEmpty(getUserName().toString())){
                    showToast(getString(R.string.login_err_name_empty));
                    username.setShakeAnimation();
                    return;
                }
                if (TextUtils.isEmpty(getPassword().toString())){
                    showToast(getString(R.string.login_err_password_empty));
                    password.setShakeAnimation();
                    return;
                }
                presenter.login();
                break;
            case R.id.forget_password:
                presenter.forgetPassword();
                break;
        }
    }
}
